home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / dos / prg / pas / swag / oop.swg / 0034_Dialogs in TVision.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  5KB  |  144 lines

  1. {
  2. >>> In a Turbo Vision DIALOG form, how do you (re)select the FIRST editable
  3. >>> data field FROM ANYWHERES IN the DIALOG?
  4.  
  5. >> You don't select it. You let IT select itself. Since all the views
  6. >> inserted into the dialog are descendents of TView, then they all
  7. >> have a select method.
  8.  
  9. > Nice Idea, too bad it's not that simple 8-(
  10.  
  11. It rarely is with TV.
  12. }
  13. Program SelectAView_2; {tested. The only thing this does, is work}
  14.  
  15.    { If you want to have an object select itself, without haveing
  16.      to explicitly define itself first, you must begin with an
  17.      object that KNOWS how to select itself.
  18.      Since Select is a method of the TView object, any descendent
  19.      will know how.
  20.  
  21.      A method is then needed by the object,
  22.      that contains the object that must select itself,
  23.      to get its, request that it select itself
  24.      to the object that must select itself.
  25.  
  26.      Use the evBroadcast event.
  27.  
  28.      The object, that contain the object that must select itself,
  29.      generates a broadcast event onto it's event tree. (random shot
  30.      in the dark) This broadcast, requests that any object that
  31.      is set to select itself on the events command, should accept the
  32.      broadcast.... , and then select itself.
  33.  
  34.      This is accomplished by taking your last instance definition
  35.      of a object that you are inserting into your event queue and
  36.      descending it once more to overide its HandleEvent method.
  37.  
  38.      In my example, I've used a simple TDialog and inserted a
  39.      bunch of of TInputLine's and a TButton that generates an
  40.      EvCommand of 'SelectFirst', and descended the HandleEvent
  41.      to generate a evBroadCast event, to broadcast the SelectFirst
  42.      Command.
  43.  
  44.      The TinputLine descendent, TMyLine, is directly descended
  45.      from the type of object that I am linking into this TDialog
  46.      objects event queue.
  47.  
  48.      Within a 'For i = 1 to 4' Loop, the TDialogs constructor
  49.      will insert a TMyLine type, that will select itself whenever
  50.      an evBroadCast event, broadcasts a SelectFirst command.
  51.  
  52.      As long as this object is a descendent of a TView, the
  53.      TDialog will accept it, and treat like any other object.
  54.  
  55.      A TButton is installed to provide a method of generating
  56.      an evBroadCast event that broadcasts a SelectFirst command.
  57.    }
  58.  
  59.  
  60. uses Objects,App,Dialogs,Views,Drivers;
  61.  
  62. type
  63.   MyDlg = object(TDialog)
  64.             constructor init;
  65.             procedure HandleEvent(var Event:TEvent); virtual;
  66.           end;
  67.  
  68.   MyLine = Object(TInputLine)
  69.              Selector : Word;
  70.              Constructor Init(var bounds:Trect;AMaxLen:Integer;
  71.                               SelectKey:Word);
  72.              Procedure HandleEvent(Var Event:TEvent); virtual;
  73.            end;
  74.   PMyLine = ^MyLine;
  75.  
  76. const
  77.   SelectFirst = 1000;
  78.  
  79. Constructor MyLine.Init(var bounds:Trect;AMaxLen:Integer;
  80.                         SelectKey:Word);
  81.    Begin
  82.      Inherited Init(Bounds,AMaxLen);
  83.      EventMask := EventMask or evBroadcast;
  84.      Selector := SelectKey;
  85.    End;
  86.  
  87. Procedure MyLine.HandleEvent(Var Event:TEvent);
  88.    Begin
  89.      inherited HandleEvent(Event);
  90.      if   (Event.What = EvBroadcast) and
  91.           (Event.Command = Selector)
  92.      then Select;
  93.   End;
  94.  
  95. Constructor MyDlg.Init;
  96.    var r:trect;
  97.        i:integer;
  98.    Begin
  99.      r.assign(0,0,50,13);
  100.      inherited init(r,'test dialog');
  101.      options := options or ofcentered;
  102.      getextent(r);
  103.      r.grow(-3,-2);
  104.      r.b.y := r.a.y + 1;
  105.      for i := 1 to 4 do
  106.         begin
  107.           if   i = 2
  108.           then insert(new(PMyLine,init(r,size.x,SelectFirst)))
  109.           else insert(New(PInputLine,init(r,size.x)));
  110.           inc(r.a.y,2); inc(r.b.y,2);
  111.         end;
  112.      inc(r.b.y);
  113.      inc(r.a.x,(size.x div 2) - 14);
  114.      dec(r.b.x,(size.x div 2) - 13);
  115.      insert(new(Pbutton,init(r,'~S~elect FirstLine',
  116.                              SelectFirst,bfdefault)));
  117.      SelectNext(False);
  118.    end;
  119.  
  120. Procedure MyDlg.HandleEvent(Var Event:TEvent);
  121.    Begin
  122.      inherited HandleEvent(Event);
  123.      if   (Event.What = EvCommand) and
  124.           (Event.Command = SelectFirst)
  125.      then Message(owner,evBroadcast,Event.Command,nil);
  126.    end;
  127.  
  128. var
  129.   a : TApplication;
  130.   m : longint;
  131. type
  132.   PMyDlg = ^MyDlg;
  133.  
  134. begin
  135.   m := memavail;
  136.   with a do
  137.   begin
  138.     Init;
  139.     ExecuteDialog(new(PMyDlg,init),nil);
  140.     done;
  141.   end;
  142.   if memavail <> m then writeln('memory allocation/deallocation error');
  143. end.
  144.